<template>
    <div class="app-container">
        <el-row :gutter="20">
            <el-col :span="6" :xs="24">
                <el-card class="box-card">
                    <div slot="header" class="clearfix">
                        <span>个人信息</span>
                    </div>
                    <div>
                        <ul class="list-group list-group-striped">
                            <li class="list-group-item">
                                <svg-icon name="user"/>
                                用户名称
                                <div class="pull-right">{{ user.userName }}</div>
                            </li>
                            <li class="list-group-item">
                                <svg-icon name="phone"/>
                                手机号码
                                <div class="pull-right">{{ user.phonenumber }}</div>
                            </li>
                            <li class="list-group-item">
                                <svg-icon name="email"/>
                                用户邮箱
                                <div class="pull-right">{{ user.email }}</div>
                            </li>
                            <li class="list-group-item">
                                <svg-icon name="tree"/>
                                所属部门
                                <div class="pull-right" v-if="user.dept">{{ user.dept.deptName }}</div>
                            </li>
                            <li class="list-group-item">
                                <svg-icon name="peoples"/>
                                所属角色
                                <div class="pull-right">{{ roleGroup }}</div>
                            </li>
                            <li class="list-group-item">
                                <svg-icon name="date"/>
                                创建日期
                                <div class="pull-right">{{ user.createTime }}</div>
                            </li>
                        </ul>
                    </div>
                </el-card>
            </el-col>
            <el-col :span="18" :xs="24">
                <el-card>
                    <div slot="header" class="clearfix">
                        <span>基本资料</span>
                    </div>
                    <el-tabs v-model="activeTab">
                        <el-tab-pane label="基本资料" name="userinfo">
                            <userInfo :user="user"/>
                        </el-tab-pane>
                        <el-tab-pane label="修改密码" name="resetPwd">
                            <resetPwd :user="user"/>
                        </el-tab-pane>
                        <el-tab-pane label="修改头像" name="resetAvatar">
                            <div class="user-info-head">
                                <img slot="open" :src="avatar" title="点击上传头像" class="img-circle img-lg"/>
                                <img-cutter ref="imgCutterModal"
                                            :label="'更换头像'"
                                            :cross-origin="true"
                                            :is-modal="true"
                                            :show-choose-btn="true"
                                            :lock-scroll="true"
                                            :box-width="900"
                                            :box-height="600"
                                            :move-able="true"
                                            :rate="'1:1'"
                                            v-on:cutDown="cutDown">
                                </img-cutter>
                            </div>
                        </el-tab-pane>
                    </el-tabs>
                </el-card>
            </el-col>
        </el-row>
    </div>
</template>

<script>
    import userInfo from "./userInfo.vue";
    import resetPwd from "./resetPwd.vue";
    import {getUserProfile, uploadAvatar} from "/src/api/system/user";
    import {store} from "/src/store";
    import ImgCutter from 'vue-img-cutter'

    export default {
        name: "Profile",
        components: {userInfo, resetPwd, ImgCutter},
        data() {
            return {
                avatar: store.getters.avatar,
                user: {},
                roleGroup: {},
                activeTab: "userinfo",
                userAvatarOpenStatus: false,
            };
        },
        created() {
            this.getUser();
        },
        methods: {
            getUser() {
                getUserProfile().then(response => {
                    this.user = response.data;
                    this.roleGroup = response.roleGroup;
                });
            },
            //更改弹出框的状态
            changeDialogStatus(status) {
                this.userAvatarOpenStatus = status
            },
            cutDown(e) {
                let formData = new FormData();
                formData.append("avatarfile", e.file);
                uploadAvatar(formData).then(response => {
                    this.avatar = response.imgUrl
                    store.commit('SET_AVATAR', response.imgUrl);
                    this.msgSuccess("修改成功");
                });
            },
        }
    };
</script>

<style lang="scss" scoped>
    .user-info-head {
        text-align: center;

        /* image */
        .img-circle {
            border-radius: 50%;
        }

        .img-lg {
            width: 400px;
            height: 400px;
        }
    }
</style>
